package edu.upc.fib.lamsandroid.client.fragment.lesson;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import com.google.gson.Gson;
import edu.upc.fib.lamsandroid.client.LamsAndroidClientApplication;
import edu.upc.fib.lamsandroid.client.Util;
import edu.upc.fib.lamsandroid.client.dialog.ImageDialog;
import edu.upc.fib.lamsandroid.client.view.URLImageView;
import edu.upc.fib.lamsandroid.server.api.model.ToolContent;
import edu.upc.fib.lamsandroid.server.api.model.ToolContentDataImageGallery;
import edu.upc.fib.lamsandroid.server.api.model.ToolContentDataImageGalleryItem;

import java.net.MalformedURLException;
import java.net.URL;

public class ImageGalleryLessonFragment extends Fragment {

    private static final int COLUMNS = 6; // TODO: calculate dynamically

    private final ToolContent toolContent;

    public ImageGalleryLessonFragment(ToolContent toolContent) {
        this.toolContent = toolContent;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Context c = getActivity().getApplicationContext();

        LinearLayout layout = new LinearLayout(c);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT, 0);

        layout.setLayoutParams(params);

        layout.setOrientation(LinearLayout.VERTICAL);

        ToolContentDataImageGallery igContent
                = new Gson().fromJson(toolContent.getData().getJson(), ToolContentDataImageGallery.class);


        Util.addWebView(getActivity(), layout, igContent.getInstructions());

        int rows = igContent.getItems().size()/COLUMNS > 0? igContent.getItems().size()/COLUMNS : 1;

        GridLayout gridLayout = new GridLayout(layout.getContext());
        gridLayout.setColumnCount(COLUMNS);
        gridLayout.setRowCount(rows);
        gridLayout.setPadding(20,20,20,20);
        layout.addView(gridLayout);

        final String urlPrefix = LamsAndroidClientApplication.getInstance().getFileRequestURLPrefix();

        int i = 0;
        for (final ToolContentDataImageGalleryItem galleryItem : igContent.getItems()) {
            URLImageView uiv = new URLImageView(getActivity());
            try {
                uiv.setImageURL(new URL(urlPrefix + galleryItem.getThumbnailFileUUID()));
                uiv.setClickable(true);
                uiv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new ImageDialog(getActivity(), urlPrefix + galleryItem.getOriginalFileUUID()).show();
                    }
                });
                gridLayout.addView(uiv, i++);
            } catch (MalformedURLException e) {
                System.out.println(e.getMessage());
                //e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }

        ScrollView scrollView = new ScrollView(getActivity());
        scrollView.addView(layout);
        return scrollView;
    }
}
